<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<!-- <cl-multi-delete-btn /> -->

			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索直播主播" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="anchorLive-info" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, reactive, ref } from "vue";
const { service } = useCool();
let anchorOptions = ref<any>([]);
// cl-upsert
const Upsert = useUpsert({
	props: {
		labelPosition: "top"
	},
	dialog: {
		width: "600"
	},
	items: [
		{
			label: "直播主播",
			prop: "anchorId",
			col: {
				span: 12
			},
			component: {
				name: "el-select",
				options: anchorOptions,
				props: { clearable: true, filterable: true }
			},
			required: true
		},
		{
			label: "直播时间",
			prop: "time",
			col: {
				span: 12
			},
			component: {
				name: "cl-date-picker",
				props: {
					type: "datetime",
					valueFormat: "YYYY-MM-DD HH:mm:ss",
					defaultTime: ""
				}
			},
			required: true
		},
		{
			label: "直播时长",
			prop: "duration",
			hook: "number",
			component: {
				name: "el-input-number",
				props: {
					controlsPosition: "right"
				}
			},
			required: true,
			col: {
				span: 12
			}
		},
		{
			label: "音浪",
			prop: "soundWaves",
			hook: "number",
			component: {
				name: "el-input-number",
				props: {
					controlsPosition: "right"
				}
			},
			required: true,
			col: {
				span: 12
			}
		},
		{
			label: "礼物数",
			prop: "gifts",
			hook: "number",
			component: {
				name: "el-input-number",
				props: {
					controlsPosition: "right"
				}
			},
			required: true,
			col: {
				span: 12
			}
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		// { type: "selection" },
		{ label: "直播主播", prop: "anchorName", minWidth: 140 },
		{
			label: "直播时间",
			prop: "time",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{ label: "直播时长", prop: "duration", minWidth: 140 },
		{ label: "音浪", prop: "soundWaves", minWidth: 140 },
		{ label: "礼物数", prop: "gifts", minWidth: 140 },
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

const GetAnchorOptions = async () => {
	let data = await service.demo.info.list();
	anchorOptions.value = data.map((o: any) => {
		return {
			value: o.id + "",
			label: o.name
		};
	});
};

// cl-crud
const Crud = useCrud(
	{
		service: service.anchorLive.info
	},
	(app) => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}

onMounted(() => {
	GetAnchorOptions();
});
</script>
